home *** CD-ROM | disk | FTP | other *** search
/ MacTech 1 to 12 / MacTech-vol-1-12.toast / Source / MacTech® Magazine / Volume 03 - 1987 / 03.01 Jan 87 / neon source / grep source next >
Encoding:
Text File  |  1986-10-07  |  5.9 KB  |  248 lines  |  [TEXT/MACA]

  1. \ © J. Langowski / MacTutor, 1986
  2. \ written in NEON v. 2.0
  3.  
  4. DECIMAL
  5. -35  CONSTANT  nsvErr        ( no such volume error ) 
  6. -43  CONSTANT  fnfErr        ( file not found error )
  7.  
  8. HEX
  9. 3F6  CONSTANT  FSFCBLen    \ >0 if HFS is being used
  10. -1   CONSTANT  MFS        \ -1 if MFS is running 
  11. ( this global variable is given for your information. )
  12. ( It is automatically checked by the NEON 2.0 word hfs? )
  13.  
  14. 10   CONSTANT  Dir/File    \ bit 4 determines whether it is
  15.                       \ a file or directory
  16.                       
  17. DECIMAL
  18.  
  19. \ Routine selectors for HFS traps 
  20. 1   CONSTANT  OpenWD        
  21. 2   CONSTANT  CloseWD        
  22. 5   CONSTANT  CatMove    
  23. 6   CONSTANT  DirCreate
  24. 7   CONSTANT  GetWDInfo
  25. 8   CONSTANT  GetFCBInfo
  26. 9   CONSTANT  GetCatInfo
  27. 10  CONSTANT  SetCatInfo
  28. 11  CONSTANT  SetVolInfo
  29. 16  CONSTANT  LockRng
  30. 17  CONSTANT  UnlockRng
  31.  
  32. \ some more constants
  33. 80  CONSTANT  FileParamSize 
  34. 106 CONSTANT  CatParamSize    
  35.  
  36. \ offsets into parameter block
  37. 12  CONSTANT  ioCompletion    ( completion routine [long word] )
  38. 16  CONSTANT  ioResult        ( result code returned here [word] )
  39. 18  CONSTANT  ioNamePtr    \ holds pointer to file name string or
  40.                           \ pathname string [long word] 
  41. 22  CONSTANT  ioVRefNum    
  42. 24  CONSTANT  FioFRefNum    ( path reference number [word] )
  43. 26  CONSTANT  FioFVersNum    ( usually zero [byte] )
  44. 28  CONSTANT  FioFDirIndex    ( index [word] )
  45. 30  CONSTANT  FioFlAttrib    ( file attributes byte [byte] )
  46. 31  CONSTANT  FioFlVersNum ( version number [byte] )
  47. 32  CONSTANT  FioFlFndrInfo    
  48. 48  CONSTANT  FioDirID    
  49. 48  CONSTANT  FioFlNum    
  50.  
  51.  
  52. 0 value Index        
  53. 0 value VolRefNum    
  54. 0 value DirID        
  55.  
  56. : ?dup dup if dup then ;
  57.  
  58. \ the HFS dispatcher...
  59. create hfsD
  60.     popD0
  61.     popA0
  62.     $ A060 w,
  63.     pushD0
  64.     next,
  65.  
  66. : $openWD { name -- VRefNum / errcode 0 }
  67.     0 fFcb ioCompletion + !
  68.     name +base fFcb ioNamePtr + !
  69.     0 fFcb FioFDirIndex + w!
  70.     fFcb +base OpenWD hfsD extend
  71.     ?dup if 0 else fFcb ioVRefNum + w@ then
  72. ;
  73.  
  74. : getixHFSfile { indx \ ResCode --  errorResCode }
  75.                                      \ setup parameter block: 
  76.     VolRefNum fFcb ioVRefNum + W!    \ specify the volume 
  77.     DirID fFcb FioDirID + !            \ pass directory id 
  78.     pad +base fFcb ioNamePtr + !     \ expect file name here 
  79.     indx fFcb FioFDirIndex + W!        \ pass current index
  80.     hfs? IF fFcb +base GetCatInfo hfsD extend -> ResCode 
  81.          ELSE fFcb fcall PBGetFInfo extend -> ResCode
  82.          THEN
  83.     ResCode   \ pass ResCode on stack
  84. ;
  85.  
  86. : NextFile  1 ++> index   index getixHFSFile ;   
  87.     
  88. 0 value level
  89.  
  90. : indent cr level 4 * spaces ;  ( for pretty printing )
  91.  
  92. : DIR    { vol  \  resCode  --   } 
  93.     0 -> Index        ( initialize index )
  94.     vol -> VolRefNum ( choose volume in internal drive )
  95.     2 -> DirID     ( specify root directory, only significant in HFS )
  96.  
  97.     BEGIN 
  98.     NextFile -> resCode
  99.     ResCode 0= 
  100.         IF 
  101.       indent
  102.       fFcb FioFlAttrib + C@    ( get the attributes byte )
  103.       Dir/File AND            ( file or directory ? )
  104.       IF    indent
  105.         33 tface ." Directory -> " pad count type
  106.         0 tface        
  107.         1 ++> level
  108.         index volrefnum     ( push on stack )
  109.          pad $openwd DIR ( recursive call to DIR )
  110.         -> volrefnum  -> index  ( pop off stack )
  111.         -1 ++> level
  112.         indent
  113.       ELSE    pad    count type
  114.       THEN
  115.         THEN
  116.     ResCode    
  117.     UNTIL    ( error found )
  118.     
  119.     ResCode ( which error ? )
  120.     CASE
  121.         fnfErr    OF
  122.         level ?dup 
  123.             IF      cr 1- 4 * spaces    
  124.                  33 tface ." End of directory" 0 tface 
  125.             ELSE cr 33 tface ." End of contents listing" 
  126.                 0 tface  quit
  127.             THEN
  128.         ENDOF
  129.     nsvErr    OF  cr ." There is no disk in drive number " vol . ENDOF
  130.     cr ." Error #" ResCode .
  131.     ENDCASE
  132. ;
  133.   
  134.   
  135. String gpattern
  136. String linBuf
  137. String target
  138.  
  139. 0 value #lines    \ total lines searched
  140.  
  141. \ SEARCHFILE - This routine searches a file for the presence of 
  142. \ gpattern, outputting all lines that contain the string.
  143. \ It is contained in the 'grep' source file on the NEON 2.0 disk.
  144. \ Please insert the appropriate source code here....
  145. \  ...
  146. \  ...
  147. \  ...
  148.  
  149. : grepInit
  150.     0 -> #lines
  151.     indent ." Searching for: " print: gPattern CR
  152.     uc: gPattern 2drop
  153.     new: linBuf
  154. ;
  155.  
  156. : grepDIR    { vol addr len \  resCode gcurs --   } 
  157.     new: loadFile new: gpattern new: target 
  158.     addr len str255 -base count put: gPattern
  159.     curs -> gcurs -curs    \ Preserve cursor status
  160.     grepInit
  161.     0 -> Index    \ initialize index  
  162.     vol -> VolRefNum \ choose volume in internal drive  
  163.     2 -> DirID  \ specify root directory , immaterial for MFS
  164.  
  165.     BEGIN ?pause
  166.     NextFile -> resCode
  167.     ResCode 0= 
  168.         IF 
  169.       indent
  170.       fFcb FioFlAttrib + C@    \ get the attributes byte  
  171.       Dir/File AND    \ file or directory?  
  172.       IF    indent
  173.         33 tface ." Directory -> " pad count type
  174.         0 tface        
  175.         1 ++> level
  176.         index volrefnum          \ push on stack  
  177.          pad $openwd addr len grepDIR 
  178.                         \ recursive call to grepDIR 
  179.         -> volrefnum  -> index       \ pop off stack 
  180.         -1 ++> level
  181.         indent
  182.       ELSE    pad    count type
  183.             pad count name: topFile
  184.             volrefnum setVref: topfile
  185.             openReadOnly: topFile ?error 132
  186.             GetFileInfo: topFile  drop
  187.             GetType: topFile txType = 
  188.             IF searchfile THEN
  189.             close: topFile drop
  190.       THEN
  191.         THEN
  192.     ResCode    \ go until error found 
  193.     UNTIL
  194.     
  195.     ResCode 
  196.     CASE
  197.         fnfErr    OF
  198.         level ?dup 
  199.             IF      cr 1- 4 * spaces
  200.                  33 tface ." End of directory" 0 tface 
  201.             ELSE cr 33 tface ." End of contents listing" 
  202.                 0 tface gcurs -> curs remove: loadfile quit
  203.             THEN
  204.         ENDOF
  205.     nsvErr OF  
  206.         cr ." There is no disk in drive number " vol . ENDOF
  207.     cr ." Error #" ResCode .
  208.     ENDCASE
  209.     remove: loadfile
  210. ;
  211.  
  212. 0 value drive
  213.  
  214. : grepone
  215.     new: loadFile
  216.     txtype 1 stdget: topfile
  217.     IF
  218.         " Enter search string:" doInDlg
  219.         IF new: gpattern new: target 
  220.         str255 -base count put: gPattern
  221.         cls grepinit
  222.         openreadonly: topfile ?error 132
  223.         searchfile
  224.         remove: loadFile
  225.         THEN
  226.     THEN
  227. ;
  228.  
  229. : grepd 0 -> level
  230.     drive " Enter search string:" 
  231.     doInDlg if cls grepdir else drop then
  232. ;
  233.  
  234. 1 menu filemenu
  235. 3 menu editmenu
  236. 7 menu grepmenu
  237.  
  238. : drive1 1 -> drive 5 uncheck: grepmenu 4 check: grepmenu ;
  239. : drive2 2 -> drive 4 uncheck: grepmenu 5 check: grepmenu ;
  240. : pron  +print 7 uncheck: grepmenu 6 check: grepmenu ;
  241. : proff -print 6 uncheck: grepmenu 7 check: grepmenu ;
  242.  
  243. : init 
  244.     " grepmenu.txt" getmtxt 
  245.     drive1 proff
  246. ;
  247.  
  248.